﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinImageTag
{
    internal class FilesINI
    {
        //ini文件
        static string IniFileName = string.Format(@"{0}\{1}.ini", Application.StartupPath, "path");

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, byte[] key, byte[] val, string filePath);

        /// <summary>
        /// 指定节点 指定键写入值
        /// </summary>
        /// <param name="Section">节点</param>
        /// <param name="Key">键</param>
        /// <param name="Value">值</param>
        /// <returns></returns>
        public static bool WriteIni(string Section, string Key, string Value)
        {
            try
            {
                WritePrivateProfileString(Section, Encoding.UTF8.GetBytes(Key), Encoding.UTF8.GetBytes(Value), IniFileName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool WriteIni(string Key, string Value)
        {
            try
            {
                return WriteIni("Config", Key, Value);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static string ReadIni(string Section, string Key)
        {
            try
            {
                byte[] Buffer = new byte[1024];
                int bufLen = GetPrivateProfileString(Section, Key, "", Buffer, Buffer.GetUpperBound(0), IniFileName);
                string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);
                return s;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 读取指定节点 指定键的值
        /// </summary>
        /// <param name="Section">节点</param>
        /// <param name="Key">键</param>
        /// <param name="FileName">ini文件</param>
        /// <returns></returns>
        public static string ReadIni(string Section, string Key, string FileName)
        {
            try
            {
                byte[] Buffer = new byte[1024];
                int bufLen = GetPrivateProfileString(Section, Key, "", Buffer, Buffer.GetUpperBound(0), FileName);
                string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);
                return s;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        public static string ReadIni(string Key)
        {
            try
            {
                byte[] Buffer = new byte[1024];
                int bufLen = GetPrivateProfileString("Config", Key, "", Buffer, Buffer.GetUpperBound(0), IniFileName);
                string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);//使用utf-8编码读取 解决乱码问题             
                return s;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    }
}
